package com.nail.api.customMake.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nail.api.customMake.bean.Address;
import com.nail.api.customMake.bean.Indent;
import com.nail.api.customMake.dao.IAddressDao;
import com.nail.api.customMake.dao.IIndentDao;
import com.nail.api.customMake.dao.IPhoneTextureDao;
import com.nail.api.customMake.dao.ITemplateDao;
import com.nail.api.customMake.service.IInDentService;
import com.nail.utils.PageResult;

@Service("inDentService")
public class InDentServiceImp implements IInDentService {
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private IIndentDao indentDao;

	@Autowired
	private IAddressDao addressDao;

	@Autowired
	private ITemplateDao templateDao;

	@Autowired
	private IPhoneTextureDao phoneTextureDao;

	@Transactional
	public void addIndent(Indent indent){
		indentDao.insertSelective(indent);
	}

	@Transactional
	public void deleteIndent(Long userId,String orderId){
		if(userId == null){
			indentDao.deleteByPrimaryKey(orderId);
		}else{
			indentDao.deleteByUserIdAndOrderId(orderId,userId);
		}
	}

	@Transactional
	public void updateIndent(Indent indent){
		indentDao.updateByPrimaryKeySelective(indent);
	}

	@Transactional
	public void setSort(String orderId,Integer sort){
		indentDao.setSort(orderId, sort);
	}

	public PageResult getIndentByUserId(Long userId, PageResult pageResult){
		List<Indent> list;
		if(userId != null) {
			list = indentDao.getIndentByUserId(userId, pageResult.getBegin(), pageResult.getPageSize());
			pageResult.setTotalNum(indentDao.getIndentByUserIdCount(userId));
		}else{
			list = indentDao.getIndent(pageResult.getBegin(), pageResult.getPageSize());
			pageResult.setTotalNum(indentDao.getIndentCountAll());
		}
		pageResult.setData(list);
		return pageResult;
	}

	public PageResult getIndentByCondition( PageResult pageResult,Map<String,Object> conditionMap){
		List<Indent> list;
		Long userId = (Long)conditionMap.get("userId");

		//拼装sql
		StringBuffer sbSelect = new StringBuffer("select * from indent where 1=1 ");
		StringBuffer sbCount = new StringBuffer("select count(1) as num from indent  where 1=1 ");

		//订单号
		if(conditionMap.get("orderId") != null){
			sbSelect.append("and order_id='"+conditionMap.get("orderId")+"' ");
			sbCount.append("and order_id='"+conditionMap.get("orderId")+"' ");
		}

		//订单状态
		if(conditionMap.get("state") != null){
			sbSelect.append("and state in("+conditionMap.get("state")+") ");
			sbCount.append("and state in("+conditionMap.get("state")+") ");
		}

		//客户名称
		if(conditionMap.get("userName") != null){
			sbSelect.append("and user_name like '%"+conditionMap.get("userName")+"%' ");
			sbCount.append("and user_name like '%"+conditionMap.get("userName")+"%' ");
		}

		//订单号
		if(conditionMap.get("orderNum") != null){
			sbSelect.append("and order_num like '%"+conditionMap.get("orderNum")+"%' ");
			sbCount.append("and order_num like '%"+conditionMap.get("orderNum")+"%' ");
		}

		//订单日期
		if(conditionMap.get("createTime") != null){
			sbSelect.append("and create_time >= '"+conditionMap.get("createTime")+"' ");
			sbCount.append("and create_time >= '"+conditionMap.get("createTime")+"' ");
		}

		if(conditionMap.get("endTime") != null){
			sbSelect.append("and create_time <= '"+conditionMap.get("endTime")+"' ");
			sbCount.append("and create_time <= '"+conditionMap.get("endTime")+"' ");
		}

		//产品类型
		if(conditionMap.get("orderType") != null){
			sbSelect.append("and order_type = "+conditionMap.get("orderType")+" " );
			sbCount.append("and order_type = "+conditionMap.get("orderType")+" ");
		}

		//用户权限
		if(userId != null){
			sbSelect.append("and user_id = "+userId +" " );
			sbCount.append("and user_id = "+userId +" ");
		}

		//状态
		/*if(conditionMap.get("state") != null){
            sbSelect.append("and state = "+conditionMap.get("state") +" " );
            sbCount.append("and state = "+conditionMap.get("state") +" ");
        }*/

		//历史订单
		if(conditionMap.get("history") != null){
			sbSelect.append("and state in (-1,3) " );
			sbCount.append("and state in (-1,3) ");
		}

		if(conditionMap.get("wait") != null){
			sbSelect.append("and state not in (-1,3) " );
			sbCount.append("and state not in (-1,3) ");
		}

		if(conditionMap.get("isDesign") != null){
			sbSelect.append(" and is_design = " + conditionMap.get("isDesign")+" ");
			sbCount.append(" and is_design = " + conditionMap.get("isDesign")+" ");
		}

		sbSelect.append("order by is_sort desc,create_time desc limit "+pageResult.getBegin()+","+pageResult.getPageSize());

		List<Indent> indents = indentDao.selectSQL(sbSelect.toString());
		for (Indent indent : indents) {
			Address address =  addressDao.selectByPrimaryKey(indent.getAddressId());
			indent.setAddress(address);
		}
		Integer count = indentDao.CountSQL(sbCount.toString());
		pageResult.setData(indents);
		pageResult.setTotalNum(count);
		return pageResult;
	}

	public Indent getIndentById(Long userId,String orderId) {
		Indent indent = null;
		if(userId == null) {
			indent = indentDao.getIndentByOrderId(orderId);
		}else {
			indent = indentDao.getIndentByOrderIdAndUserId(orderId,userId);
		}   
		if(indent.getTemplateId() != null) {
			indent.setTemplate(templateDao.selectByPrimaryKey(indent.getTemplateId()));    		
		}
		if(indent.getPhoneTextureId() != null) {
			indent.setPhoneTexture(phoneTextureDao.selectByPrimaryKey(indent.getPhoneTextureId()));  		
		}
		return indent;
	}

	@Transactional
	public void changeState(Long userId,String orderId,Integer state) {
		String sql;
		if(userId == null) {
			sql = "update indent set state = " + state + " where order_id = " + orderId;
		}else {
			sql = "update indent set state = " + state + " where order_id = " + orderId + " and user_id ="+userId;
		}  
		indentDao.changeState(sql);
	}

	@Transactional
	public void changePayState(Long userId,String orderId,Integer payStatus){
		indentDao.changePayState(userId,orderId,payStatus);
	}

	@Transactional
	public void setIsDown(String orderId){
		indentDao.setIsDown(orderId);
	}

}
